package com.example.travelservlet.plan.service;

import com.example.travelservlet.entity.Plan;
import com.example.travelservlet.plan.repository.PlanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 30697
 */
@Service
public class PlanServiceImpl implements PlanService{
    @Autowired
    private PlanRepository planRepository;



    @Override
    public Plan createPlan(Plan plan) {
        return  planRepository.save(plan);
    }

    @Override
    public Plan updatePlan(Plan plan) {
        Plan update = planRepository.saveAndFlush(plan);
        return update;
    }

    @Override
    public List<Plan> findPlansByTotalPlanId(String totalPlanId) {
        return planRepository.findPlansByTotalPlanId(totalPlanId);
    }


    @Override
    public List<Plan> findPlanByBeginTimeAndAndTotalPlanId(String beginTime, String totalPlanId) {
        Specification specification = new Specification<Plan>(){
            @Override
            public Predicate toPredicate(Root<Plan> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date dateTime = format.parse(beginTime);
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String dateStr = dateFormat.format(dateTime);
                    predicates.add(criteriaBuilder.equal(root.get("beginTime"),dateStr));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return planRepository.findAll(specification);
    }

    @Override
    public void deleteByPlanId(String planId) {
        planRepository.deleteByPlanId(planId);
    }

    @Override
    public Plan findPlanByPlanId(String planId) {
        Plan planByPlanId = planRepository.findPlanByPlanId(planId);
        return planByPlanId;
    }

    @Override
    public void setPlanToUsing(String planId) {
        Plan plan = planRepository.findPlanByPlanId(planId);
        planRepository.setUseIndexByPlanId(plan.getTotalPlanId());
        planRepository.updateUseIndexByPlanId(planId, 1);
    }


//    @Override
//    public List<Plan> findPlanBySequenceAndTotalPlanId(Plan plan,String totalPlanId) {
//        Specification specification = new Specification<Plan>() {
//            @Override
//            public Predicate toPredicate(Root<Plan> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//                List<Predicate> predicates = new ArrayList<>();
//                predicates.add(criteriaBuilder.equal(root.get("sequence"),1));
//                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
//            }
//        };
//        Sort.Order order = Sort.Order.asc("beginTime");
//        Sort sort = Sort.by(order);
//        return planRepository.findAll(specification,sort);
//
//    }

}
